package com.bobo.web.provider.util.commutils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * Author: zhangyong
 * Date: 2016/11/15
 */
public class TimeHelper {

    public static DateTimeFormatter ISO_DATETIME = ISODateTimeFormat.dateTime();

    public static DateTimeFormatter ISO_DATETIME_NOMILLIS = ISODateTimeFormat.dateTimeNoMillis();

    public static DateTimeFormatter VIPKID_DATETIME = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter ISO_DATETIME_TIME_ZONE_FORMAT =  DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");

    public static DateTimeFormatter CHINESE_FORMAT = DateTimeFormat.forPattern("yyyy年MM月dd日HH点mm分");

    private static ZoneId CHINA_ZONE_ID = ZoneId.of("Asia/Shanghai");

    public static Date parse(DateTimeFormatter dateTimeFormater, String dateStr) {
        return dateTimeFormater.parseDateTime(dateStr).toDate();
    }

    public static String print(DateTimeFormatter dateTimeFormater, Date date) {
        return dateTimeFormater.print(date.getTime());
    }

    public static String print(Date date) {
        return print(VIPKID_DATETIME, date);
    }

    public static String format(Date date){return print(ISO_DATETIME_TIME_ZONE_FORMAT,date);}


    public static Date parse(String dateStr) {
        return parse(VIPKID_DATETIME, dateStr);
    }

    public static class DateDeserializer extends JsonDeserializer<Date> {

        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext context)
                throws IOException, JsonProcessingException {
            String dateStr = jsonParser.getText();
            return TimeHelper.parse(TimeHelper.VIPKID_DATETIME, dateStr);
        }
    }

    public static class DateSerializer extends JsonSerializer<Date> {

        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
            jsonGenerator.writeString(TimeHelper.print(TimeHelper.VIPKID_DATETIME, date));
        }
    }

    public static class IOSDateSerializer extends JsonSerializer<Date> {

        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
            jsonGenerator.writeString(TimeHelper.print(TimeHelper.ISO_DATETIME_TIME_ZONE_FORMAT, date));
        }
    }


    public static class UnixTimeStampDeserializer extends JsonDeserializer<Date> {

        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            Long unixTimestamp = jsonParser.getLongValue();
            return (unixTimestamp != null && unixTimestamp > 0L) ? new Date(unixTimestamp * 1000) : null;
        }
    }

    public static class UnixTimeStampSerializer extends JsonSerializer<Date> {

        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            long unixTimeStamp = 0L;
            if (date != null) {
                unixTimeStamp = date.getTime() / 1000;
            }
            jsonGenerator.writeNumber(unixTimeStamp);
        }
    }

    public static Date fromLocalDateTimeToDate(LocalDateTime time){
        return Date.from(time.atZone(CHINA_ZONE_ID).toInstant());
    }

    public static  LocalDateTime fromDateToLocalDateTime(Date time){
        return time.toInstant().atZone(CHINA_ZONE_ID).toLocalDateTime();
    }

}
